<!DOCTYPE html>
<html>
<head>
    <title>攻略系列</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/bootstrap-4.1.1-dist/css/bootstrap.min.css">
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap-4.1.1-dist/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/strategyCatalogs.css"/>
    <script src="/js/plugins/jrender/jrender.min.js"></script>
    <link rel="stylesheet" href="/js/plugins/dialog2/dialog.css"/>
    <script src="/js/plugins/dialog2/dialog.min.js"></script>
    <script src="/js/common.js"></script>
    <script>
        var userStr = sessionStorage.getItem("user");
        var user;
        if (userStr) {
            user = JSON.parse(userStr);
        }
        var greatStatus;//点赞状态
        var enshrineStatus;//收藏状态
        var signId; //标记对象的id

        var strategyId = getParams().id;
        var currentPage = 1;//当前页
        var totalPage;//总页数
        var travelArr = [];//存放页面的容器
        var loading = false;//判定请求数据是否传完的标记

        function query() {

            //页头搜索功能
            $("#searchBtn").click(function () {
                var keyword = $("#search").val();
                if(keyword!=null&keyword!=""){
                    window.location.href="/searchPage.html?keyword="+keyword+"&type=1";
                }else{
                    $(document).dialog({
                        titleText: '温馨提示',
                        content: '搜索内容不能为空!',
                    });
                }
            });


            if (loading) {//如果是加载状态就不发送请求
                return;
            }
            loading = true;//进入加载状态
            //查询该攻略下的所有评论
            $.get(baseUrl + "strategies/" + strategyId + "/comments", {currentPage: currentPage}, function (data) {
                //每次查询都要把页面信息合并到容器中
                $.merge(travelArr, data.list);
                var json = {list: travelArr};
                $("#comment").renderValues(json, {
                    getHref: getHref
                });
                //绑定图片弹框事件
                $(".comment-img img").click(function () {
                    $(document).dialog({
                        titleShow: false,
                        content: $(this).prop("outerHTML")
                    });
                });


                //没查询一次当前页数加1
                currentPage++;
                //获取数据的总页数
                totalPage = data.pages;
                loading = false;//加载完成后把状态改成可以发送请求的状态
            })

        }

        $(function () {

            //查询大攻略对象,,回显大攻略名称
            $.get(baseUrl + "strategies/" + strategyId, function (data) {
                $(".title").html(data.title)
            });
            //查询攻略类别
            $.get(baseUrl + "strategies/" + strategyId + "/catalogs", function (data) {
                var index = 1;
                $(".catalogs").renderValues({list: data}, {
                    getHref: getHref,
                    getName: function (item, value) {
                        if (index < 10) {
                            $(item).html("0" + index++ + "." + value);
                        } else {
                            $(item).html(index++ + "." + value);
                        }
                    }
                });
            });
            //默认自动查询第一页数据
            query();
            $(window).scroll(function () {
                //如果页面的滚动长度加窗口长度等于页面的总长度时 发送请求查询下一页数据
                if ($(document).scrollTop() + $(window).height() >= $(document).height() - 50) {//提前五十像素发送请求
                    //当前页小于总页数的时候才查询
                    if (currentPage <= totalPage) {
                        query();
                    } else {
                        $(document).dialog({
                            type: 'notice',
                            infoIcon: '../img/icon/loading.gif',
                            infoText: '不要拉了,再拉就出屎了!',
                            autoClose: 2500
                        });
                    }

                }
            })
            //如果是用户查询标记状态
            if (user) {
                $.get(baseUrl + "strategySigns/" + strategyId, function (data) {
                    if (data) {
                        //如果有新建过sign对象获取id
                        signId = data.id;

                    }
                    //回显标记状态
                    if (data.greatStatus) {
                        $(".great").removeClass("fa-thumbs-o-up").addClass("fa-thumbs-up");
                        greatStatus = true;
                    }
                    if (data.enshrineStatus) {
                        $(".enshrine").removeClass("fa-heart-o").addClass("fa-heart");
                        enshrineStatus = true;
                    }
                });

            }
            //查询当前攻略的点赞和收藏总数
            $.get(baseUrl + "strategySigns/count/" + strategyId, function (data) {
                //渲染数据
                $(".strategies").renderValues(data);

            });
            //跳转点评编辑界面
            $("#commentBtn").click(function () {
                window.location.href = "/mine/addComment.html?id=" + strategyId;
            });


            //绑定点赞和收藏点击效果
            $(".enshrine").click(function () {
                //如果不是用户跳转到登录页面
                if (!user) {
                    $(document).dialog({
                        type: 'confirm',
                        closeBtnShow: true,
                        content: '登录之后才能收藏哦!',
                        onClickConfirmBtn: function () {
                            window.location.href = "/login.html";
                        }
                    });

                } else {
                    //如果是用户则改变收藏的动画效果并改变收藏的状态
                    changeClass($(this), "fa-heart-o", "fa-heart");
                    enshrineStatus = !enshrineStatus;
                    var json = {
                        'strategy.id': strategyId,
                        enshrineStatus: enshrineStatus,
                        greatStatus: greatStatus
                    };
                    //发送请求更新用户的标记状态和更新大攻略的收藏总数
                    if (!signId) {//根据标记对象id判断是发送新增还是更新请求
                        //新增
                        $.post(baseUrl + "strategySigns", json, function (data) {
                            $("#enshrineNum").html(data.data.enshrineStatus);
                        });
                        $.get(baseUrl + "strategySigns/count/" + strategyId, function (data) {
                            //回显收藏总数
                            $("#enshrineNum").html(data.greatStatus);
                        });
                    } else {
                        //更新
                        $.ajax({
                            url: baseUrl + "strategySigns/" + signId,
                            method: "put",
                            data: json,
                            success: function (data) {
                                //回显收藏总数
                                $("#enshrineNum").html(data.data.enshrineStatus);
                            }
                        })

                    }

                }

            });

            $(".great").click(function () {
                if (user) {
                    //如果是用户则改变点赞的动画效果并改变点赞的状态
                    changeClass($(this), "fa-thumbs-o-up", "fa-thumbs-up");
                    greatStatus = !greatStatus;
                    var json = {
                        'strategy.id': strategyId,
                        enshrineStatus: enshrineStatus,
                        greatStatus: greatStatus

                    };
                    //发送请求更新用户的标记状态和更新大攻略的点赞总数
                    if (!signId) {//根据标记对象id判断是发送新增还是更新请求
                        //新增
                        $.post(baseUrl + "strategySigns", json, function (data) {
                            if (data.success) {
                                //更新点赞总数
                                $("#greatNum").html(data.data.greatStatus);
                            }
                        })


                    } else {
                        //更新
                        $.ajax({
                            url: baseUrl + "strategySigns/" + signId,
                            method: "put",
                            data: json,
                            success: function (data) {
                                if (data.success) {
                                    //更新点赞总数
                                    $("#greatNum").html(data.data.greatStatus);
                                }
                            }
                        })
                    }
                } else {
                    //如果不是用户跳转到登录页面
                    $(document).dialog({
                        type: 'confirm',
                        closeBtnShow: true,
                        content: '登录之后才能点赞哦!',
                        onClickConfirmBtn: function () {
                            window.location.href = "/login.html";
                        }
                    });


                }

            })


        })
    </script>

</head>

<body>
<div class="search-head">
    <div class="row nav-search">
        <div class="col-1">
            <a href="index.html">
                <span><i class="fa fa-chevron-left fa-2x"></i></span>
            </a>
        </div>
        <div class="col-9">
            <div class="input-group-sm search">
                <input id="search" class="form-control" placeholder="找攻略">
            </div>
        </div>
        <div class="col-1">
            <i class="fa fa-search" id="searchBtn"
               style="color:white;font-size:25px;position: absolute;top: 23%;left: 88%">
            </i>
        </div>
    </div>
</div>

<div class="container strategies">
    <h5 class="title"></h5>
    <small><span>0</span>人阅读过此攻略</small>
    <div class="focus">
        <i class="fa fa-heart-o enshrine"><span render-html="enshrineStatus" id="enshrineNum"> 0</span> 收藏</i>
        <i class="fa fa-thumbs-o-up great"><span render-html="greatStatus" id="greatNum"> 0</span> 点赞</i>
        <button class="btn">想去</button>
        <button class="btn">去过</button>
    </div>
</div>
<ul class="nav nav-pills nav-justified border border-left-0 border-top-0 border-right-0" id="pills-tab">
    <li class="nav-item ">
        <a class="nav-link active" id="pills-catalog-tab" data-toggle="pill" href="#pills-catalog">
            <span>攻略目录</span>
        </a>
    </li>
    <li class="nav-item">
        <a class="nav-link " id="pills-comment-tab" data-toggle="pill" href="#pills-comment">
            <span>骡友点评</span>
        </a>
    </li>
</ul>
<div class="tab-content " id="pills-tabContent">
    <div class="tab-pane fade show active" id="pills-catalog">

        <div class="catalogs">
            <div render-loop="list">
                <div class="container detail">
                    <h6 render-key="list.name" render-fun="getName"></h6>
                    <div render-loop="list.details">
                        <a data-url="strategyDetails.html?id=" render-fun="getHref" render-key="list.details.id">
                            <button class="btn" render-html="list.details.title"></button>
                        </a>
                    </div>
                </div>
            </div>
        </div>

    </div>
    <div class="tab-pane fade " id="pills-comment">

        <div class="container tag">
            <h6>骡友标签</h6>
            <div>
                <span>价格便宜</span>
                <span>很热闹</span>
                <span>交通方便</span>
                <span>吃的很多</span>
                <span>风景优美</span>
                <span>美食很多</span>

            </div>
        </div>
        <hr>

        <div id="comment">
            <div render-loop="list">
                <div class="container row comment">
                    <div class="col-2 comment-head">
                        <a data-url="/userProfiles.html?id=" render-fun="getHref" render-key="list.user.id">
                            <img class="rounded-circle"
                                 render-src="list.user.headImgUrl">
                        </a>
                    </div>
                    <div class="col comment-right">
                        <p class="authorName"><span render-html="list.user.nickName"></span></p>
                        <span class="comment-star" render-html="list.star">
                            <span class="comment-date" render-html="creatTime"></span>
                            </span>
                        <span class="comment-date" render-html="list.createTime"></span>

                        <div class="comment-content">
                            <a data-url="strategyComment.html?id=" render-fun="getHref" render-key="list.id">
                                <p render-html="list.content"></p>
                            </a>
                        </div>

                        <ul class="comment-img" render-loop="list.imgUrlsArr">
                            <li>
                                <img render-src="list.imgUrlsArr.self" width="100%"/>
                            </li>
                        </ul>

                        <div class="d-flex icon">
                            <a data-url="/mine/strategyComment.html?id=" render-fun="getHref" render-key="list.id">
                                <i class="fa  fa-commenting-o commentGreat" style="margin-right: 10px" > 评论 <span
                                        render-html="list.commentNum"> 0</span></i>
                            </a>
                        </div>

                    </div>
                </div>

            </div>

        </div>
    </div>
    <div class="operation">
        <button class="btn">加入旅行单</button>
        <button class="btn" id="commentBtn">点评</button>
    </div>
</div>


</body>
</html>